﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Configuration;
using System.Threading;

namespace TY_Hoist.WinApp.Code
{
    /// <summary>
    /// 遥控器控制类
    /// </summary>
    public class RemoteControl : SerialPort
    {
        private List<byte> buffer = new List<byte>(4096);
        public RemoteControl()
        {

            this.PortName = ConfigurationManager.AppSettings["RCComNumber"].ToString();
            this.BaudRate = 9600;
            this.Parity = Parity.None;
            this.DataBits = 8;
            this.StopBits = StopBits.One;
            //this.RtsEnable = true;
            //this.ReadBufferSize = 1024;
            //this.WriteBufferSize = 512;
            //this.DtrEnable = true;
            //this.Encoding = Encoding.GetEncoding("GB2312");

            this.Open();

        }



        /// <summary>
        /// 蜂鸣器
        /// </summary>
        public void Beep(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_1_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_1_OFF);
            }
        }

        /// <summary>
        /// 电机正转
        /// </summary>
        public void MotorRight(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_3_ON);
                this.CmdRead(8);
                this.CmdSend(RelayCmd.RELAY_6_ON);
                this.CmdRead(8);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_3_OFF);
                this.CmdRead(8);
                this.CmdSend(RelayCmd.RELAY_6_OFF);
                this.CmdRead(8);
            }
            this.Clear();
        }

        /// <summary>
        /// 电机反转
        /// </summary>
        public void MotorLeft(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_4_ON);
                this.CmdRead(8);
                this.CmdSend(RelayCmd.RELAY_5_ON);
                this.CmdRead(8);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_4_OFF);
                this.CmdRead(8);
                this.CmdSend(RelayCmd.RELAY_5_OFF);
                this.CmdRead(8);

            }
            this.Clear();
        }

        /// <summary>
        /// 相机触发
        /// </summary>
        public void Camera(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_8_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_8_OFF);
            }
        }

        /// <summary>
        /// 点火
        /// </summary>
        public void DianHuo(bool swich)
        {
            if (swich)
            {
                this.CmdSend(RelayCmd.RELAY_7_ON);
            }
            else
            {
                this.CmdSend(RelayCmd.RELAY_7_OFF);
            }
        }

        /// <summary>
        /// 遥控器停止
        /// </summary>
        public string Ceshi()
        {

            this.CmdSend(RelayCmd.RELAY_2_OFF);
            this.CmdRead(8);
            this.Clear();
            return this.CmdRead(8);
        }

        /// <summary>
        /// 查询是否点火开始
        /// </summary>
        public bool SearchStart()
        {
            this.CmdSend(RelayCmd.RELAY_SEARCH);
            byte[] buffer1 = new Byte[this.BytesToRead];   //创建缓冲区
            Thread.Sleep(10);
            this.Read(buffer1, 0, buffer1.Length);
            string ss;
            ss = byteToHexStr(buffer1); //用到函数byteToHexStr
            if (ss.Substring(0, 6) == "010201")
            {
                var val = System.Convert.ToString(strToToHexByte(ss.Substring(6, 2))[0], 2).PadLeft(3, '0');
                var status = val.Substring(0, 1);
                return status == "1" ? true : false;
            }
            else {

                return false;
            }


        }

        /// <summary>
        /// 查询是否点火结束
        /// </summary>
        public bool SearchStop()
        {

            this.CmdSend(RelayCmd.RELAY_SEARCH);
            byte[] buffer1 = new Byte[this.BytesToRead];   //创建缓冲区
            Thread.Sleep(10);
            this.Read(buffer1, 0, buffer1.Length);
            string ss;
            ss = byteToHexStr(buffer1); //用到函数byteToHexStr
            if (ss.Substring(0, 6) == "010201")
            {
                var val = System.Convert.ToString(strToToHexByte(ss.Substring(6, 2))[0], 2).PadLeft(3, '0');
                var status = val.Substring(1, 1);
                return status == "1" ? true : false;
            }
            else
            {

                return false;
            }

        }

        /// <summary>
        /// 查询开关状态
        /// </summary>
        public bool SearchSwitch()
        {
            this.CmdSend(RelayCmd.RELAY_SEARCH);
            byte[] buffer1 = new Byte[6];   //创建缓冲区
            Thread.Sleep(10);
            this.Read(buffer1, 0, buffer1.Length);
            string ss;
            ss = byteToHexStr(buffer1); //用到函数byteToHexStr
            var val = System.Convert.ToString(strToToHexByte(ss.Substring(6, 2))[0], 2).PadLeft(3, '0');
            return val.Substring(2, 1) == "1" ? true : false;

        }

        /// <summary>
        /// 发送串口命令
        /// </summary>
        /// <param name="cmd"></param>
        public void CmdSend(byte[] cmd)
        {
            buffer = new List<byte>(4096);
            try
            {
                this.Write(cmd, 0, cmd.Length);
            }
            catch (Exception)
            {

                DSkin.Forms.DSkinMessageBox.Show("通信失败！");
                // throw;
            }

        }
        public string CmdRead(int length)
        {
            byte[] buffer1 = new Byte[length];   //创建缓冲区
            Thread.Sleep(10);
            this.Read(buffer1, 0, buffer1.Length);
            string ss;
            ss = byteToHexStr(buffer1); //用到函数byteToHexStr
            return ss;

        }
        /// <summary>
        /// 清空缓存命令
        /// </summary>
        public void Clear()
        {
            this.DiscardInBuffer();
            this.DiscardOutBuffer();
            buffer.Clear();

        }
        /// <summary>
        /// 释放串口
        /// </summary>
        public new void Dispose()
        {
            if (this.IsOpen)
            {
                this.Close();
            }
            this.Dispose(true);
        }

        //字节数组转16进制字符串
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }
        private static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
    }
}
